﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace SGArchiveSystem7xDB.Linq
{
    /// <summary>
    /// 词典模块
    /// </summary>
    public class T_ArchiveWordTypeDal
    {
        static SGArchiveSystem7xEntities conn;
        /// <summary>
        /// 添加一个词典类型
        /// </summary>
        /// <param name="en"></param>
        /// <returns></returns>
        public static _ReturnValue Add_ArchiveWordType(T_ArchiveWordType en)
        {
            try
            {
                conn = new SGArchiveSystem7xEntities();
                if (conn.T_ArchiveWordType.Select(c => c).Where(c => c.WT_NAME == en.WT_NAME).Count() > 0)
                    return _ReturnValue.Already;
                else
                {
                    conn.T_ArchiveWordType.Add(en);
                    conn.SaveChanges();
                    return _ReturnValue.OK;
                }
            }
            catch { return _ReturnValue.Err; }
        }

        /// <summary>
        /// 修改一个词典类型
        /// </summary>
        /// <param name="id">要修改的ID</param>
        /// <param name="name">修改后的词典名称</param>
        /// <returns></returns>
        public static _ReturnValue Modify_ArchiveWordType(int id, string name)
        {
            try
            {
                conn = new SGArchiveSystem7xEntities();
                var query = conn.T_ArchiveWordType.Select(c => c).Where(c => c.WT_ID == id);
                foreach (var n in query)
                    n.WT_NAME = name;
                conn.SaveChanges();
                return _ReturnValue.OK;
            }
            catch { return _ReturnValue.Err; }
        }

        /// <summary>
        /// 删除一个词典类型
        /// </summary>
        /// <param name="id">要删除的ID</param>
        /// <returns>如果返回 Already, 表示是系统的无法删除</returns>
        public static _ReturnValue Del_ArchiveWordType(int id)
        {
            try
            {
                conn = new SGArchiveSystem7xEntities();
                var query = conn.T_ArchiveWordType.Select(c => c).Where(c => c.WT_ID == id);
                foreach (var n in query)
                {
                    if (n.WT_SYSTEM == 1)
                        return _ReturnValue.Already;
                    else
                        conn.T_ArchiveWordType.Remove(n);
                }
                conn.SaveChanges();
                return _ReturnValue.OK;
            }
            catch { return _ReturnValue.Err; }
        }

        /// <summary>
        /// 获取词典类型，词典类型无需翻页
        /// </summary>
        /// <returns></returns>
        public static List<T_ArchiveWordType> Get_ArchiveWordType(int status=-1)
        {
            try
            {
                conn = new SGArchiveSystem7xEntities();
                var query = conn.T_ArchiveWordType.Select(c => c);
                if (status > -1)
                    query = query.Where(c => c.WT_STATUS == status);
                return query.OrderBy(c=>c.WT_ID).ToList();
            }
            catch { return null; }
        }
    }
}
